package com.huazai.apply.manager.web.action;


import com.huazai.apply.manager.bean.User;
import com.huazai.apply.manager.dao.UserDao;
import com.huazai.apply.manager.dao.impl.UserDaoImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author EZTTU
 */
@WebServlet(name = "Apply", value = {"/add", "/decide", "/delete"})
public class ApplyServlet extends HttpServlet {
    private static final String SERVICE_ADD = "/add";
    private static final String SERVICE_DECIDE = "/decide";
    private static final String SERVICE_DELETE = "/delete";
    private static final String RESULT_PRE = "Prepared";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String servletPath = request.getServletPath();
        HttpSession session = request.getSession(false);
        User userMessage = (User) session.getAttribute("UserMessage");
        if (userMessage != null) {
            if (userMessage.getUserField().equals("admin")) {

                if (servletPath.equals(SERVICE_DECIDE)) {
                    this.doDecide(request, response);
                } else if (servletPath.equals(SERVICE_DELETE)) {
                    this.doDelete(request, response);
                } else {
                    response.sendRedirect(request.getContextPath());
                }

            } else {

                if (servletPath.equals(SERVICE_ADD)) {
                    this.doAdd(request, response);
                } else if (servletPath.equals(SERVICE_DELETE)) {
                    this.doDelete(request, response);
                } else {
                    response.sendRedirect(request.getContextPath());
                }

            }
        } else {
            response.sendRedirect(request.getContextPath());
        }

    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {

        UserDao userDao = new UserDaoImpl();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();

        //0012-12-12T20:12,注意前端的传入数据是这样子的，所以需要以下的语句来进行修正
        String userid = request.getParameter("userid");
        String dateTime = request.getParameter("dateTime");
        String wantTime = request.getParameter("outTime");

        String replaceTime = dateTime.replace("T", " ");
        replaceTime += ":00";


        //replaceTime用于解决时间格式不匹配而出此下策。
        if (userDao.uploadSubmit(userid, replaceTime, Integer.parseInt(wantTime), RESULT_PRE)) {
            pw.print("<h1 align=\"center\">申请提交成功</h1>");
        } else {
            pw.print("<h1 align=\"center\">申请提交失败</h1>");
        }
    }


    private void doDecide(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserDao userDao = new UserDaoImpl();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        String applyId = request.getParameter("applyID");
        String result = request.getParameter("result");

        if (userDao.updateSubmit(applyId, result)) {
            pw.print("<h1 align=\"center\">审批成功</h1>");
            response.sendRedirect(request.getContextPath() + "/admin");
        } else {
            pw.print("<h1 align=\"center\">审批失败</h1>");
            pw.print("<a href='javascript:void(0)' onclick='window.history.back()'>返回上一级</a>");
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserDao userDao = new UserDaoImpl();
        String applyId = request.getParameter("applyID");
        HttpSession session = request.getSession();
        User userMessage = (User) session.getAttribute("UserMessage");


        if (userDao.deleteSubmit(applyId)) {
            if (userMessage.getUserField().equals("admin")) {
                response.sendRedirect(request.getContextPath() + "/admin");
            } else {
                response.sendRedirect(request.getContextPath() + "/user");
            }
        } else {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.print("<h1 align='center'>出校申请删除失败</h1>");
            pw.print("<a href='javascript:void(0)' onclick='window.history.back()'>返回上一级</a>");
        }
    }

}
